Object invoke(Object target, String name, Object args[])
throws Exception {
Class at[] = getArgumentTypes(args);
Class clazz = target.getClass();
Method mems[] = clazz.getMethods();
// find the most specific applicable method of clazz
Method best = null;
for (int i = 0; i < mems.length; i++) {
if (!mems[i].getName().equals(name)) continue;
Class pt[] = mems[i].getParameterTypes();
if (isMoreSpecific(at, pt)) {
// it's applicable (the name & param types match)
if (best == null ||
isMoreSpecific(pt, best.getParameterTypes()))
best = mems[i];
}
}
// execute the chosen method
return best.invoke(target, args);
}
|